package com.ruoyi.quartz.controller;

import com.ruoyi.base.yaml.QuartzConfig;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 调度任务信息操作处理
 * wqg: 说明这个Controller只是负责转发到另一个系统（独立的定时任务系统）
 * @author ruoyi
 */
@Slf4j
@RestController
@RequestMapping("/monitor/job")
public class SysJobController extends BaseController {

    @Autowired
    private ISysJobService jobService;

    private static String lcreditUrl = QuartzConfig.lcreditUrl;
    private static RestTemplate restTemplate;

    static {
        log.info("lcreditUrl:{}",lcreditUrl);
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(180000);
        factory.setReadTimeout(180000);
        restTemplate = new RestTemplate(factory);
    }

    /**
     * 查询定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysJob sysJob) {
            startPage();
        Map<String, Object> map = new HashMap<>();
        map.put("jobName",sysJob.getJobName());
        map.put("jobGroup",sysJob.getJobGroup());
        map.put("status",sysJob.getStatus());
        TableDataInfo tableDataInfo = restTemplate.getForObject(
                lcreditUrl+"lcredit/monitor/job/list?jobName={jobName}&jobGroup={jobGroup}&status={status}"
                ,TableDataInfo.class,map);
        return tableDataInfo;
    }

    /**
     * 导出定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:export')")
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysJob sysJob) {
        List<SysJob> list = jobService.selectJobList(sysJob);
        ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);
        return util.exportExcel(list, "定时任务");
    }

    /**
     * 获取定时任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:query')")
    @GetMapping(value = "/{jobId}")
    public AjaxResult getInfo(@PathVariable("jobId") Long jobId) {
        return AjaxResult.success(jobService.selectJobById(jobId));
    }

    /**
     * 新增定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:add')")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysJob sysJob){
        sysJob.setCreateBy(SecurityUtils.getUsername());
        AjaxResult res =restTemplate.postForObject(lcreditUrl+"lcredit/monitor/job",sysJob,AjaxResult.class);
        return res;
    }

    /**
     * 修改定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:edit')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysJob sysJob) {
        sysJob.setUpdateBy(SecurityUtils.getUsername());
        AjaxResult res =restTemplate.postForObject(lcreditUrl+"lcredit/monitor/job/edit",sysJob,AjaxResult.class);
        return res;
    }

    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysJob sysJob){
        AjaxResult res =restTemplate.postForObject(lcreditUrl+"lcredit/monitor/job/changeStatus",sysJob,AjaxResult.class);
        return res;
    }

    /**
     * 定时任务立即执行一次
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/run")
    public AjaxResult run(@RequestBody SysJob sysJob){
        AjaxResult res =restTemplate.postForObject(lcreditUrl+"lcredit/monitor/job/run",sysJob,AjaxResult.class);
        return res;
    }

    /**
     * 删除定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public AjaxResult remove(@PathVariable String jobIds){
//        jobService.deleteJobByIds(jobIds);
//        return AjaxResult.success();
        AjaxResult res =restTemplate.postForObject(lcreditUrl+"lcredit/monitor/job/"+jobIds,null,AjaxResult.class);
        return res;
    }
}
